<?php
namespace app\admin\controller;

use think\Request;

class Index extends Base
{
    public function index()
    {
        $data = \app\admin\model\AdminUser::get($this->uid);

        $lgdata =  \app\admin\model\LoginNote::field('time,ip,browser')->order('id desc')->limit(10)->select();
        $lgcounts =  \app\admin\model\LoginNote::where('userid', $this->uid)->count();

        $this->assign('lgdata', $lgdata);
        $this->assign('lgcounts', $lgcounts);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function getSiteSize()
    {
        $dirname = ROOT_PATH;
        $dirsize = totdir($dirname);
    
        define('BKM', 1024);

        if ($dirsize>BKM) {
            $dirsize =  round($dirsize/BKM, 2);

            if ($dirsize>BKM) {
                $dirsize = round($dirsize/BKM, 2);
            
                $dirsize .="M";
            } else {
                $dirsize .="K";
            }
        } else {
            $dirsize .="B";
        }

        return ['code'=>2, 'size'=>$dirsize];
    }

    public function clearTemp()
    {
        $tempsize = round(totdir(TEMP_PATH)/1024, 2);

        $tempdir = \scandir(TEMP_PATH);
        // \var_dump($tempdir);
        
        $res = true;
        foreach ($tempdir as $val) {
            if ($val == '.' or $val == '..') {
                continue;
            }
            $res = \unlink(TEMP_PATH . $val);
        }

        $cachesize = round(totdir(CACHE_PATH)/1024, 2);

        deldir(CACHE_PATH);

        $size = $tempsize + $cachesize;

        if (!$res) {
            return ['code'=>0, 'msg'=>"清除失败, 请稍后重试"];
        } else {
            return ['code'=>2, 'msg'=>"操作成功, 共清理 " . $size . 'K 缓存'];
        }
    }
}
